package com.zzh.web;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.Button;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zzh.base.component.BaseActivity;
import com.zzh.tools.log.ToolLog;
import com.zzh.web.jsbridge.BridgeHandler;
import com.zzh.web.jsbridge.BridgeWebView;
import com.zzh.web.jsbridge.CallBackFunction;
import com.zzh.web.jsbridge.Constant;
import com.zzh.web.jsbridge.DefaultHandler;

public class WebActivity extends BaseActivity implements View.OnClickListener {


    private BridgeWebView mBridgeWebView;
    private Button mBtWeb;

    @Override
    public View getRootView() {
        return LayoutInflater.from(this).inflate(R.layout.activity_web, null);
    }

    @Override
    public void findView() {

        mBtWeb = findBaseViewById(R.id.bt_web);
        mBridgeWebView = findBaseViewById(R.id.bridge_web_view);
        init();
    }

    private void init() {
        configWebView();
    }

    private void configWebView() {

        mBridgeWebView.setDefaultHandler(new DefaultHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {
                super.handler(data, function);
                ToolLog.i("zzh", ">>>data>>>" + data);
            }
        });
        mBridgeWebView.setWebChromeClient(new WebChromeClient() {

            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                ToolLog.i("-----------------------------------------------");
                ToolLog.i(String.valueOf(newProgress));
                ToolLog.i("-----------------------------------------------");
            }

            // 以下是打开文件的老方法
            @SuppressWarnings("unused")
            public void openFileChooser(ValueCallback<Uri> uploadMsg, String AcceptType, String capture) {
                this.openFileChooser(uploadMsg);
                ToolLog.i("zzh", ">>>openFileChooser1>>>>");
            }

            @SuppressWarnings("unused")
            public void openFileChooser(ValueCallback<Uri> uploadMsg, String AcceptType) {
                this.openFileChooser(uploadMsg);
                ToolLog.i("zzh", ">>>openFileChooser2>>>>");
            }

            public void openFileChooser(ValueCallback<Uri> uploadMsg) {
                ToolLog.i("zzh", ">>>openFileChooser3>>>>");
            }

            // 显示文件的回调
            @Override
            public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
                ToolLog.i("zzh", ">>>onShowFileChooser>>>>");
                return super.onShowFileChooser(webView, filePathCallback, fileChooserParams);
            }
        });

//        mBridgeWebView.loadUrl("file:///android_asset/demo.html");
        mBridgeWebView.loadUrl("http://192.168.86.137:8989");

        mBridgeWebView.registerHandler(Constant.REGISTER_HANDLER_VALUE, new BridgeHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {
                ToolLog.i("zzh", "handler = submitFromWeb, data from web = " + data);
                function.onCallBack("submitFromWeb exe, response data 中文 from Java");
            }
        });

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name", "zhang");
        jsonObject.put("age", "123");
        jsonObject.put("six", "321");

        mBridgeWebView.callHandler("functionInJs", JSON.toJSONString(jsonObject),
                new CallBackFunction() {
                    @Override
                    public void onCallBack(String data) {
                        ToolLog.i("zzh", ">>>>>onCallBack>>>>>>");
                    }
                });
        mBridgeWebView.send("hello");

    }

    @Override
    public void setClickLinsener() {
        mBtWeb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mBridgeWebView.callHandler("functionInJs", "data from Java", new CallBackFunction() {

                    @Override
                    public void onCallBack(String data) {
                        // TODO Auto-generated method stub
                        ToolLog.i("zzh", "reponse data from js " + data);
                    }

                });
            }
        });
    }

    @Override
    public void superBefore() {
        super.superBefore();

    }

    @Override
    public void superAfter() {
        super.superAfter();

        hiddenTitle();
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    }

    @Override
    protected void onNewIntent(Intent intent) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {

        }
        return super.onKeyDown(keyCode, event);
    }
}